package com.swm.rest;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpRecoverableException;
import org.apache.commons.httpclient.methods.GetMethod;

public class HttpGetMethod {

	private static HttpGetMethod getMethod = null;
	public static HttpGetMethod getInstance(){
		if ( getMethod == null ){
			getMethod = new HttpGetMethod();
		}
		return getMethod;
	}
	/**
	 * 
	 */
	public ResultService request( String url ) {
		HttpClient client = new HttpClient();		
		HttpMethod method = new GetMethod(url);		
        int statusCode = -1;   
                
        // We will retry up to 3 times.
        for (int attempt = 0; statusCode == -1 && attempt < 3; attempt++) {
            try {
                // execute the method.
                statusCode = client.executeMethod(method);
            } catch (HttpRecoverableException e) {
                System.err.println("A recoverable exception occurred, retrying.  " + e.getMessage());
            } catch (IOException e) {
                System.err.println("Failed to download file.");
                e.printStackTrace();
                System.exit(-1);
            }
        }
        
        // Check that we didn't run out of retries.
        if (statusCode == -1) {
            System.err.println("Failed to recover from exception.");
            System.exit(-2);
        }
        
        byte[] responseBody = method.getResponseBody();        
        method.releaseConnection();        
		
        String response;
        if ( responseBody == null ) {
        	response = "";
        }
        else{
        	response = new String( responseBody, 0, responseBody.length );				  
        }
		
        System.out.println( response );
		return new ResultService( statusCode, response );
	}
	
	
	public ResultService request( String url, String xml ){

        
        HttpClient client = new HttpClient();		
        GetMethod method = new GetMethod(url);		

		InputStream is = new ByteArrayInputStream( xml.getBytes() );
		method.setRequestHeader("Content-type", "application/xml; charset=utf-8");
		//method.setFileData(fileData)            
        
		
		int statusCode = -1;   
                
        // We will retry up to 3 times.
        for (int attempt = 0; statusCode == -1 && attempt < 3; attempt++) {
            try {
                // execute the method.
                statusCode = client.executeMethod(method);
            } catch (HttpRecoverableException e) {
                System.err.println("A recoverable exception occurred, retrying.  " + e.getMessage());
            } catch (IOException e) {
                System.err.println("Failed to download file.");
                e.printStackTrace();
                System.exit(-1);
            }
        }
        
        // Check that we didn't run out of retries.
        if (statusCode == -1) {
            System.err.println("Failed to recover from exception.");
            System.exit(-2);
        }
        
        byte[] responseBody = method.getResponseBody();        
        method.releaseConnection();        
		
        String response;
        if ( responseBody == null ) {
        	response = "";
        }
        else{
        	response = new String( responseBody, 0, responseBody.length );				  
        }
		
        System.out.println( response );
		return new ResultService( statusCode, response );
	}
}
